package cn.edu.Controller;

import cn.edu.DAO.AdminDAOByXML;
import cn.edu.DAO.OrderDAOByXML;
import cn.edu.DAO.UserDAOByXML;
import cn.edu.DAO.travelshopByXML;
import cn.edu.DO.OrderDO;
import cn.edu.DO.TravelShopDO;
import cn.edu.DO.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Controller
public class UserController {
    @Autowired
    UserDAOByXML userDAO;
    @Autowired
    AdminDAOByXML adminDAO;
    @Autowired
    OrderDAOByXML orderDAO;
    @Autowired
    travelshopByXML travelshopDAO;
    @RequestMapping("/login")
    public String toLogin(){
        return "user/login1";
    }
//    @RequestMapping("/deleteUser")
//    public String DeleteUser(){
//        return "admin/DeleteUser";
//    }
    @RequestMapping("/loginController")
    public ModelAndView login(HttpServletRequest request, Model model){
        String username = request.getParameter("username");
        String passwd = request.getParameter("passwd");
        ModelAndView mv = new ModelAndView();
        if(adminDAO.queryAdminToLogin(username,passwd)!=null){
            mv.setViewName("admin/AdminIndex");
        }
        else if(userDAO.queryUserToLogin(username,passwd)!=null){
            mv.setViewName("user/UserIndex");
        }
        else{
            model.addAttribute("error","用户名或密码错误");
            mv.setViewName("user/login");
        }
        return mv;
    }
    @RequestMapping("/loginCheck")
    public String login(UserDO user, Map map, HttpSession session){
        if(userDAO.queryUserToLogin(user.getId(),user.getPassword())!=null){
            UserDO user1=userDAO.selectUserById(user.getId());
            session.setAttribute("username",user1.getUsername());
            session.setAttribute("password",user1.getPassword());
            session.setAttribute("id",user1.getId());
            map.put("id",session.getAttribute("id"));
            map.put("username",session.getAttribute("username"));
            return "user/UserIndex";
        }
        else{
            map.put("error","用户名或密码错误");
        }
        return "user/login1";
    }
//    @RequestMapping("/adminUserAdd")
//    public String adminUserAdd(){
//        return "admin/UserAdd";
//        }
    @RequestMapping("/fallback")
    public String backAdminIndex(){
        return  "admin/AdminIndex";
    }

    @RequestMapping("/shop")
    public String toShop(Model model){
        List<TravelShopDO> travels = new ArrayList<>();
        travels = travelshopDAO.selectAll();
        model.addAttribute("list2",travels);
        return "common/ShopDetail";
    }
    @RequestMapping("/index")
    public String toindex(){
        return "index";
    }
    @RequestMapping("/placeDetail")
    public String toplaceDetail(HttpServletRequest request,Model model){
        String sid=request.getParameter("sid");
        TravelShopDO commdity=travelshopDAO.selectShopBySid(sid);
        model.addAttribute("commdity",commdity);
        return "common/placeDetail";
    }
    @RequestMapping("/regist")
    public String toregist(){
        return "user/register";
    }
    @RequestMapping("/userOrder")
    public String adduserOrder(HttpServletRequest request,HttpSession session,Model model){
        String sid=request.getParameter("sid");
        String id = (String) session.getAttribute("id");
        model.addAttribute("sid",sid);
        model.addAttribute("id",id);
        return "user/addOrder";
    }
    @RequestMapping("/addO")
    public String addO(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        String sid=request.getParameter("sid");
        String oid = request.getParameter("oid");
        String date = request.getParameter("date");
        OrderDO order=new OrderDO();
//        Random random=new Random();
//        int a=random.nextInt()*100;
//        String oid= String.valueOf(a);
//        LocalDate date1 = LocalDate.now();
//        String date=date1.toString();
//        OrderDO order=new OrderDO();
//        order.setId(id);
//        order.setSid(sid);
//        order.setDate(date);
//        order.setOid(oid);
       orderDAO.insertOrder(order);
        return "common/add";

    }
    @RequestMapping("/toOrder")
    public String toOrder(){
        return "user/addOrder";
    }
    @RequestMapping("/add")
    public String toadd(){
        return"common/add";
    }


}
